﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="~/plugins/mzui/js/mzui.min.js"></script>
    <link href="~/plugins/mzui/css/mzui.min.css" rel="stylesheet" />
    <title>购买卡密</title>
</head>
<body class="with-nav-bottom">
    <div id="kami" class="hidden">
        <textarea id="km" style="width:800px;height:800px;"></textarea>
    </div>
    <div id="gmkm">
        <div class="section">
            <div class="heading">
                <div class="title text-center"><h4><button class="btn">@LJSheng.Common.LCookie.GetU("account"), <span class="label label-badge black">请选择套餐</span></button></h4></div>
            </div>
            <div class="box">
                <div class="outline">
                    <div class="list" id="tbody">
                        <script id="tpl" type="text/html">
                            {{each list as item index}}
                            <a class="item with-avatar multi-lines" onclick="set('{{item.name}}','{{item.gid}}','{{item.rmb}}')">
                                <div class="content">
                                    <div class="title"><div class="label text-tint circle primary">{{item.name}}</div></div>
                                    <div class="subtitle"></div>
                                </div>
                                <div class="content">
                                    <div class="title">{{item.xfx}} 天</div>
                                    <div class="subtitle"></div>
                                </div>
                                <div class="content">
                                    <div class="title">{{item.rmb}} 元</div>
                                    <div class="subtitle"></div>
                                </div>
                            </a>
                            <div class="divider"></div>
                            {{/each}}
                        </script>
                    </div>
                </div>
            </div>
            <div id="lxwm" class="affix dock-bottom modal" style="margin-bottom:60px;">
                <div class="heading">
                    <div class="title"><strong style="color:red;">购买数量</strong> <i class="icon icon-plus" onclick="add(1);"></i> <input type="number" class="form-control" id="gmsl" value="1" placeholder="最低购买数量1" style="width:58px;text-align:center;" onkeyup="value=value.replace(/[^\d]/g,'');"> <i class="icon icon-minus" onclick="add(-1);"></i> <span id="msg" class="label label-primary label-outline blue">最少购买数量 1</span></div>
                </div>
            </div>
        </div>
        <nav class="nav affix dock-bottom justified">
            <a id="grade" class="red">请选择套餐</a><input type="text" id="gid" class="hidden" /><input type="text" id="rmb" class="hidden" />
            <a id="wx" href="javascript:gopay();" class="btn green"><label style="color:blue;font-size:18px;">余额支付</label></a>
        </nav>
    </div>
    <label id="pageindex" class="hidden">0</label><label id="pages" class="hidden">1</label>
    <label id="Controllers" class="hidden">/home/zctc</label>
    <script src="~/js/template-web.js"></script>
    <script src="~/js/mlist.js"></script>
    <script>
        $("#gmsl").on('input', function () {
            if (jctc()) {
                add(0);
            }
        });

        function jctc() {
            if ($("#rmb").val() > 0) {
                return true;
            }
            else {
                $("#gmsl").val("1");
                $("#msg").html("请选择套餐");
                $("#grade").html("请选择套餐").addClass("black").removeClass("red");
            }
            return false;
        }

        function add(n) {
            if (jctc()) {
                $("#grade").removeClass("black").addClass("red");
                $("#gmsl").val(Number($("#gmsl").val()) + n);
                if (Number($("#gmsl").val()) <= 0) {
                    $("#gmsl").val("1");
                    $("#msg").html("最少购买数量 1");
                }
                else {
                    var jg = $("#rmb").val() * $("#gmsl").val();
                    var rmb = Math.round(jg * 100) / 100;
                    $("#msg").html("需要支付" + rmb);
                    if (@ViewBag.rmb < jg) {
                        $("#gmsl").val("1");
                        $("#msg").html("余额不足,无法购买更多!");
                    }
                }
            }
        }

        function set(grade,gid,rmb)
        {
            $("#gmsl").val("1");
            $("#rmb").val(rmb);
            $("#gid").val(gid);
            $("#grade").html(grade).addClass("active text-info").removeClass("red");
        }

        function gopay() {
            if ($("#grade").html() != "请选择套餐" && $("#gid").val() != "" && $("#gmsl").val() != "") {
                $.ajax({
                    url: "/ajax/api.ashx",
                    data: "ff=gopay&shopgid=" + $("#gid").val() + "&gmsl=" + $("#gmsl").val() + "&Cache=" + Math.random(),
                    type: "post",
                    cache: false,
                    timeout: 8000,
                    dataType: "json",
                    ContentType: "application/json; charset=utf-8",
                    async: false,//一定要是同步请求，否则会跳转；（ajax默认是异步的）
                    error: function () { document.getElementById("wx").innerHTML = "余额支付"; document.getElementById("wx").href = "javascript:gopay();"; alert("AJAX Error"); },
                    beforeSend: function () { document.getElementById("wx").innerHTML = "提交订单中..."; document.getElementById("wx").href = "javascript:void(0);"; },
                    success: function (data) {
                        document.getElementById("wx").innerHTML = "余额支付";
                        document.getElementById("wx").href = "javascript:gopay();";
                        if (data.appId != "payerr") {
                            switch (data.return_msg) {
                                case "ok":
                                    $("#gmkm").addClass("hidden");
                                    $("#kami").removeClass("hidden");
                                    self.location = "/home/msg?url=/shop/index&msg=购买成功!";
                                    break;
                                default:
                                alert("支付失败,返回信息:" + data.err_msg);
                                    break;
                                }
                        }
                        else {
                            alert(data.return_msg);
                            if (data.return_msg == "请先登录")
                            {
                                self.location = "/home/denglu";
                            }
                        }
                    },
                });
            }
            else
            {
                $("#grade").addClass("red");
            }
        }
    </script>
</body>
</html>